﻿using AutoMapper;
using B.S.Basic.Domain;
using B.S.Basic.ErrorCode;
using B.S.Basic.Infratrucate;
using MD5Hash;
using MediatR;

namespace B.S.Basic.Write.API.Application
{
    public class AddCommandHandler : IRequestHandler<AddCommand, APIResult<object>>
    {
        private readonly IBaseRepositroy<User> useRepository;
        private readonly IMapper mapper;
        public AddCommandHandler(IBaseRepositroy<User> useRepository, IMapper mapper)
        {
            this.useRepository = useRepository;
            this.mapper = mapper;
        }
        public Task<APIResult<object>> Handle(AddCommand request, CancellationToken cancellationToken)
        {
            APIResult<object> result=new APIResult<object>();
            result.Code=EnumCode.添加成功;
            result.Message=EnumCode.添加成功.ToString();

            var entity = mapper.Map<User>(request);
            entity.UPass = request.UPass.GetMD5();
            useRepository.Add(entity);


            return Task.FromResult(result);
        }
    }
}
